When reading data from a relational database (e.g., the result set of a query), a driver first receives metadata about the structure and constraints of the data followed by a stream of raw data blocks from the database. The driver then decodes the received raw data into data items that can be delivered to the user. This step must be done for each value of each row of every column in the result set of a query.
Database drivers for statically typed programming languages such as Java require the user to provide the expected output type when decoding a value. The following code snippet shows an abstract example in Java using the Java Database Connectivity (JDBC) API:
ResultSet rs = stmt.executeQuery(“SELECT * FROM students WHERE . . .”);rs.next( );int studentId = rs.getInt(2);
In the above example, a query is submitted to a database system through JDBC and a result set handle is returned. With that handle, a user can explore the metadata and fetch data into the user's Java program. When fetching data from a result set, a driver is responsible for getting the raw data of rows from the database systems (e.g. “rs.next( )”) and for decoding values into a form that can be represented in Java. In statically typed languages, the specification of how to decode a value is given by the user. For example, the call “rs.getInt(2)” explicitly tells the driver to get the value of the column with index 2 in form of a Java integer.
In contrast, dynamically typed programming languages (e.g., JavaScript, Ruby, Python) do not provide ways to specify the data type that a column of a row should be converted into. Data types are dynamically inferred and verified by the language at runtime. The following code snippet shows how reading a column value might look like in JavaScript:
var resultSet = stmt.executeQuery(“SELECT * FROM students WHERE . . .”);resultSet.next( );var studentId = resultSet.get(2);
In contrast to statically typed programming languages such as Java where the user provides the expected output type when decoding a value, the type of the “studentId” column in the above code example is decided by the driver at runtime and is typically based on some well-documented default mapping between the database types and the language types. The driver exploits the metadata (e.g. database types, column constraints) provided by the database to automatically decode the received raw bytes into a value of the appropriate type.
The interpretation of metadata to select the correct data decoding method is a performance critical part of a database driver. It typically involves interpretation cost on every retrieval of a column from a result set, e.g., the implementation would have to use lookups at run-time and might look like as follows:
function get(var index) {  if (metadata.getType(index) == dataTypes.NUMBER)   return metadata.mayBeNull(index) ?    getDoubleOrNULLFromNumber :     getDoubleFromNumber(index); else if (metadata.getType(index) == dataTypes. BINARY_DOUBLE)   return metadata.mayBeNull(index) ?    getDoubleOrNULLFromBinaryDouble : getDoubleFrom-    BinaryDouble(index);  else if (metadata.getType(index) == dataTypes.VARCHAR)   return metadata.mayBeNull(index) ? getStringOrNull( ) :    getString(index);  else if (metadata.getType(index) == dataTypes.CLOB)   return metadata.mayBeNull(index) ? getCLOBOrNull(index) :    getCLOB(index);  else if   . . .}
As shown in the above code example, when a user requests a value from the driver, the driver performs several lookups to determine the specific function that performs the correct decoding for the value. For example, if the source data type of a requested value is VARCHAR with the constraint NOT NULL, the function get(1) would dispatch to getString(1) that decodes the raw data into a JavaScript string.
The key issue with this approach to decoding for dynamic programming languages is that determining the correct decoding function must be done for every value a user requests, imposing significant overhead as result.
Based on the foregoing, an approach for reducing overhead of decoding operations using dynamically typed languages is desired.